package com.qdsg.ysg.doctor.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.nurse.activity.AtyWaitAccept;
import com.qdsg.ysg.doctor.ui.widget.FlowAllLayoutManager;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.response.DocOrderListVO;
import com.rest.response.NurseItemsVo;
import com.rest.response.PatientProjectDTO;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.LogUtil;

/**
 * Description
 * Created by lbj on 2022/4/9 9:56
 */
public class ExtraListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private Boolean isFromAccept;
    private Map<String, List<PatientProjectDTO.NaAdditionalConsumablesDTO>> mExtraList;
    private List<String> mKeyList;
    private OnItemClickLitener mOnItemClickLitener;


    public ExtraListAdapter(Context mContext, Map<String, List<PatientProjectDTO.NaAdditionalConsumablesDTO>> mExtraList, List<String> mKeyList, Boolean isFromAccept) {
        this.mContext = mContext;
        this.mExtraList = mExtraList;
        this.mKeyList = mKeyList;
        this.isFromAccept = isFromAccept;
    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from( context )
                .inflate( R.layout.item_nurse_extra, parent, false );
        return new ViewHolder( view );
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder mHolder = (ViewHolder) holder;

        List<PatientProjectDTO.NaAdditionalConsumablesDTO> consumablesDTO = mExtraList.get( mKeyList.get( position ) );
        assert consumablesDTO != null;
        double mAllPrice = 0;
        List<PatientProjectDTO.NaAdditionalConsumablesDTO> mItems = new ArrayList<>();
        mItems.clear();
        mItems.addAll( consumablesDTO );
        for (PatientProjectDTO.NaAdditionalConsumablesDTO additionalConsumablesDTO : consumablesDTO) {
            double mul = UtilString.mul( Double.parseDouble( additionalConsumablesDTO.price ), Double.parseDouble( additionalConsumablesDTO.number ) );
            mAllPrice = UtilString.add( mul, mAllPrice );
        }
        try {
            double div = UtilString.div( mAllPrice, 100.00, 3 );
            mHolder.tvExtraPrice.setText( "¥" + div );
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        String status = consumablesDTO.get( 0 ).orderStatus;
        String orderNumber = consumablesDTO.get( 0 ).orderNumber;
        mHolder.tvOrderNumber.setText( "订单编号：" + orderNumber );
        LogUtil.e( "耗材状态--" + status );
        switch (status) {
            case "10":
                mHolder.tvExtraStatus.setText( "未支付" );
                break;
            case "20":
                mHolder.tvExtraStatus.setText( "已取消" );
                break;
            case "30":
                mHolder.tvExtraStatus.setText( "支付中" );
                break;
            case "35":
                mHolder.tvExtraStatus.setText( "已支付" );
                break;
        }
        if ("10".equals( status )) {
            if (isFromAccept) {
                mHolder.tvCancelExtra.setVisibility( View.VISIBLE );
            }
            mHolder.tvExtraStatus.setTextColor( ContextCompat.getColor( mContext, R.color.fff2a37d ) );
        } else if ("30".equals( status ) || "35".equals( status )) {
            mHolder.tvCancelExtra.setVisibility( View.GONE );
            mHolder.tvExtraStatus.setTextColor( ContextCompat.getColor( mContext, R.color.ffa8d2cb ) );
        } else {
            mHolder.tvCancelExtra.setVisibility( View.GONE );
            mHolder.tvExtraStatus.setTextColor( ContextCompat.getColor( mContext, R.color.ff666666 ) );
        }


        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        mHolder.rvExtra.setLayoutManager( manager );

        NurseExtraItemsAdapter extraItemsAdapter = new NurseExtraItemsAdapter( mContext, mItems );
        mHolder.rvExtra.setAdapter( extraItemsAdapter );

        mHolder.tvCancelExtra.setOnClickListener( v -> {
            if (mOnItemClickLitener != null) {
                mOnItemClickLitener.onItemClick( mKeyList.get( position ) );
            }
        } );


    }

    @Override
    public int getItemCount() {
        return mKeyList.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(String orderId);
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_order_number)
        TextView tvOrderNumber;
        @BindView(R.id.tv_extra_status)
        TextView tvExtraStatus;
        @BindView(R.id.rv_extra)
        RecyclerView rvExtra;
        @BindView(R.id.tv_extra_price)
        TextView tvExtraPrice;
        @BindView(R.id.tv_cancel_extra)
        TextView tvCancelExtra;

        public ViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }
    }


}
